<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>百变空间的轮播图</title>
    <style>
        ul {
            list-style: none
        }
        li {
            position: absolute;
            left: 0;
            top: 0;
            width: 100%;
            height: 484px;
        }
    </style>
</head>
<body>
    <div id="banner">
        <ul>
            <li><img src="img/banner1.jpg"></li>
            <li><img src="img/banner2.jpg"></li>
            <li><img src="img/banner3.jpg"></li>
            <li><img src="img/banner4.jpg"></li>
            <li><img src="img/banner5.jpg"></li>
            <li><img src="img/banner6.jpg"></li>
        </ul>
    </div>
    <script>
        main();

        function main() {
            var obj1 = document.getElementById('banner');
            var li = obj1.getElementsByTagName('li');
            var j = 0; // 循环li图片
            var iIndex = 1; // zindex
            var timer; // 时间差

            // 把第一张图片设置为不透明，其他图片设置为透明
            for(var i = 0; i < li.length; i++) {
                if(i == j) {
                    li[i].style.opacity = '1';
                    continue; // 直接执行下次循环的代码，不用管下面的代码
                }
                li[i].style.opacity = '0';
            };

            // 开始轮播
            function startRoll() { // 循环执行把图片的图层置前
                j++;
                if(j >= li.length) {
                    j = 0;
                }
                li[j].style.zIndex = iIndex;
                iIndex++;
                fadeIn(li[j], 1);
            };

            function fadeIn(obj, period) {
                var COUNT = 50;
                clearInterval(obj.timer);
                obj.timer = setInterval(function() { // 把最上面的图片慢慢设置为不透明
                    var opacityValue = Number.parseFloat(obj.style.opacity);
                    opacityValue += period/COUNT;
                    if(opacityValue < 1) {
                        obj.style.opacity = opacityValue;
                    }else {
                        obj.style.opacity = 1;
                        clearInterval(obj.timer);
                        for(var i = 0; i < li.length; i++) {
                            if(i == j) {
                                continue;
                            }
                            li[i].style.opacity = '0';
                        }
                    }
                }, 1000/COUNT);
            }
            
            timer = setInterval(startRoll, 2000);
        }
    </script>
</body>
</html>